package com.wude.guild;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.mc.developmentkit.utils.ToastUtil;
import com.umeng.analytics.MobclickAgent;
import com.wude.bean.MyCouponBean;
import com.wude.bean.UserInfo;
import com.wude.guild.adapter.MyCouponAlreadyAdapter;
import com.wude.guild.adapter.MyCouponExpiredAdapter;
import com.wude.guild.adapter.MyCouponNotAdapter;
import com.wude.guild.servercore.mints.TitleBaseActivity;
import com.wude.guild.views.MyListView;
import com.wude.http.HttpCom;
import com.wude.tools.Utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 我的优惠券
 */
public class MyCouponActivity extends TitleBaseActivity {

    @BindView(R.id.tv_coupon_not)
    TextView tvCouponNot;
    @BindView(R.id.lv_coupon_not)
    MyListView lvCouponNot;
    @BindView(R.id.tv_coupon_already)
    TextView tvCouponAlready;
    @BindView(R.id.lv_coupon_already)
    MyListView lvCouponAlready;
    @BindView(R.id.tv_coupon_expired)
    TextView tvCouponExpired;
    @BindView(R.id.lv_coupon_expired)
    MyListView lvCouponExpired;
    @BindView(R.id.tv_hint_not)
    TextView tvHintNot;
    @BindView(R.id.tv_hint_already)
    TextView tvHintAlready;
    @BindView(R.id.tv_hint_expired)
    TextView tvHintExpired;

    private List<MyCouponBean.DataBean.NotuseBean> listNotuse = new ArrayList<>();
    private List<MyCouponBean.DataBean.OvertimeBean> listOvertime = new ArrayList<>();
    private List<MyCouponBean.DataBean.UseBean> listUsed = new ArrayList<>();
    private MyCouponNotAdapter couponNotAdapter = new MyCouponNotAdapter(this);
    private MyCouponAlreadyAdapter couponAlreadyAdapter = new MyCouponAlreadyAdapter(this);
    private MyCouponExpiredAdapter couponExpiredAdapter = new MyCouponExpiredAdapter(this);
    private final String TAG = "MyCouponActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_coupon);
        ButterKnife.bind(this);

        setHeaderTitle("我的优惠券");

        Map<String, String> map = new HashMap<>();
        UserInfo info = Utils.getLoginUser();
        map.put("token", info.token);
        HttpCom.POST(myCouponHandler, HttpCom.getMyCouponList(), map, false);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    /**
     * 获取我的优惠券列表
     */
    @SuppressLint("HandlerLeak")
    private Handler myCouponHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    try {
                        Log.e("我的优惠券列表返回的数据:", msg.obj.toString());
                        MyCouponBean bean = new Gson().fromJson(msg.obj.toString(), MyCouponBean.class);
                        if (bean.getStatus() == 1) {
                            if (bean.getData().getNotuse().size() > 0) {
                                listNotuse.addAll(bean.getData().getNotuse());
                                couponNotAdapter.setList(listNotuse);
                                lvCouponNot.setAdapter(couponNotAdapter);
                                tvCouponNot.setText("未使用" + "(" + bean.getData().getNotuse().size() + ")");
                                tvHintNot.setVisibility(View.GONE);
                            } else {
                                tvHintNot.setVisibility(View.VISIBLE);
                                tvCouponNot.setText("未使用(0)");
                            }

                            if (bean.getData().getUse().size() > 0) {
                                listUsed.addAll(bean.getData().getUse());
                                couponAlreadyAdapter.setList(listUsed);
                                lvCouponAlready.setAdapter(couponAlreadyAdapter);
                                tvCouponAlready.setText("已使用" + "(" + bean.getData().getUse().size() + ")");
                                tvHintAlready.setVisibility(View.GONE);
                            } else {
                                tvHintAlready.setVisibility(View.VISIBLE);
                                tvCouponAlready.setText("已使用(0)");
                            }

                            if (bean.getData().getOvertime().size() > 0) {
                                listOvertime.addAll(bean.getData().getOvertime());
                                couponExpiredAdapter.setList(listOvertime);
                                lvCouponExpired.setAdapter(couponExpiredAdapter);
                                tvCouponExpired.setText("已过期" + "(" + bean.getData().getOvertime().size() + ")");
                                tvHintExpired.setVisibility(View.GONE);
                            } else {
                                tvHintExpired.setVisibility(View.VISIBLE);
                                tvCouponExpired.setText("已过期" + "(0)");
                            }
                        }
                    } catch (Exception e) {
                        ToastUtils.showShortToast("数据解析异常");
                        Log.e("我的优惠券列表异常：", e.toString());
                    }
                    break;
                case 2:
                    ToastUtil.showToast("网络错误");
                    break;
            }
        }
    };
}
